﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;
using DACNPM;

namespace DACNPM.MyUserControl.UCHienThi
{
    public partial class UCThemNV : UserControl
    {
        NumberBUO numBUO = new NumberBUO();
        NhanVienBUO nvBUO = new NhanVienBUO();
        String idNV;
        int id;
        public UCThemNV()
        {
            InitializeComponent();
            dataGridView1.DataSource = nvBUO.GetAll();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if ((txtTen.Text.Trim() == "") || (txtDiaChi.Text.Trim() == "") || (txtSDT.Text.Trim() == ""))
            {
                MessageBox.Show("Chưa nhập đủ thông tin!");
            }
            else
            {
                if (numBUO.isNumberInput(txtSDT.Text) == false)
                {
                    MessageBox.Show("Trường số điện thoại phải nhập số!");
                }
                else
                {
                    tbl_NhanVien nv = new tbl_NhanVien();
                    nv.TenNhanVien = txtTen.Text;
                    nv.SoDienThoai = txtSDT.Text;
                    nv.DiaChi = txtDiaChi.Text;
                    nv.NgaySinh = dateTimePicker1.Text;
                    if (nvBUO.Insert(nv) == true)
                    {
                        dataGridView1.DataSource = nvBUO.GetAll();
                        MessageBox.Show("Thêm mới nhân viên thành công");
                        txtTen.Text = "";
                        txtSDT.Text = "";
                        txtDiaChi.Text = "";
                        UCThemNV ucThemNV = new UCThemNV();
                        Form form = this.ParentForm;
                        ControlAccess.ShowConTrol(form, ucThemNV, "grbHienThiChucNang");
                    }
                    else
                    {
                        MessageBox.Show("Có lỗi xảy ra khi thêm mới nhân viên");
                    }
                }
            }
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            txtTen.Text = "";
            txtSDT.Text = "";
            txtDiaChi.Text = "";
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        idNV = dataGridView1.Rows[id].Cells[0].Value.ToString();


                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void danhSáchNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCDanhSachNV ucDSNV = new UCDanhSachNV();

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, ucDSNV, "grbHienThiChucNang");
        }

        private void sửaNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCSuaNV ucSuaNV = new UCSuaNV(idNV);

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, ucSuaNV, "grbHienThiChucNang");
        }

        private void xóaNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

    }
}
